// src/mock/index.ts
import Mock from 'mockjs';

// 模拟用户数据
const users = Mock.mock({
    'list|100': [
        {
            'id|+1': 1,
            username: '@word(5,10)',
            nickname: '@cname',
            email: '@email',
            phone: /^1[3-9]\d{9}$/,
            avatar: "@image('100x100', '#4A7BF7', 'Avatar')",
            createTime: '@datetime',
        },
    ],
}).list;

// 登录接口
Mock.mock(/\/api\/login/, 'post', (options) => {
    const { username, password } = JSON.parse(options.body);
    if (
        (username === 'admin'  && password === 'jg688517') ||
        (username === '15660163699' && password === 'jg688517') ||
        (username === '19850366050' && password === '1214849609') ||
        (username === '18567183010' && password === 'jg688517') ||
        (username === '16696302900' && password === 'jg688517') ||
        (username === '13083775636' && password === 'jg688517') ||
        (username === '15565658695' && password === 'jg688517') ||
        (username === '18737798677' && password === 'jg688517') ||
        (username === '18521082991' && password === 'jg688517') ||
        (username === '18737702827' && password === 'jg688517') ||
        (username === '13782087180' && password === 'jg688517') ||
        (username === '18737108297' && password === 'jg688517')
    ) {
        return {
            code: 200,
            message: '登录成功',
            data: {
                token: Mock.Random.guid(),
                userInfo: {
                    id: 1,
                    username: username,
                    nickname: username,
                    avatar: 'https://gw.alipayobjects.com/zos/antfincdn/XAosXuNZyF/BiazfanxmamNRoxxVxka.png',
                },
            },
        };
    }
    return {
        code: 500,
        message: '用户名或密码错误',
    };
});

// 获取用户列表
Mock.mock(/\/api\/users/, 'get', (options) => {
    const { url } = options;
    const params = new URLSearchParams(url.split('?')[1]);
    const page = parseInt(params.get('page') || 1);
    const pageSize = parseInt(params.get('pageSize') || 10);
    const username = params.get('username');

    let filteredUsers = users;
    if (username) {
        filteredUsers = users.filter((user) =>
            user.username.includes(username)
        );
    }

    const start = (page - 1) * pageSize;
    const end = page * pageSize;
    const total = filteredUsers.length;
    const list = filteredUsers.slice(start, end);

    return {
        code: 200,
        message: '成功',
        data: {
            list,
            total,
        },
    };
});

// 获取当前用户信息
Mock.mock(/\/api\/currentUser/, 'get', () => {
    return {
        code: 200,
        message: '成功',
        data: {
            id: 1,
            username: 'admin',
            nickname: '管理员',
            avatar: 'https://gw.alipayobjects.com/zos/antfincdn/XAosXuNZyF/BiazfanxmamNRoxxVxka.png',
            email: 'admin@example.com',
            phone: '13800138000',
            roles: ['admin'],
        },
    };
});
